package com.ruoyi.web.controller.api;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.alibaba.fastjson2.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.ruoyi.common.config.RuoYiConfig;

import java.io.File;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;


public class TestZfb {

    public static void main(String[] args) throws AlipayApiException {
      // createUrl("11111111111",new BigDecimal(12));
//        getBase64("");
        String url = "www.example.com";
        String hexString = bytesToHex(url.getBytes(StandardCharsets.UTF_8));
        System.out.println(hexString);
    }

    private static String bytesToHex(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            String hex = Integer.toHexString(0xff & b);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append("%");
            hexString.append(hex);
        }
        return hexString.toString();
    }

    public static String createUrl(String orderNo, BigDecimal amount) throws AlipayApiException {

        String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwkB9Kpp5o4Gwx+EWK3Qw71a8+ay4xbHpnsZ+DnPt7gu6tpphrrAQ8usKvZE5m++kARrQ5017l3TKmENfTQMr2Ygfb4NNPqE6ejCyDDrwgGgrO+mDKQmwdJqVwZFDQwoKmGxkLMwbGLjpZwwkoDY45J+YftPYsXJEofj7zlf6l2ruHA81LU5AQ0l634gqot84MYmzqr8RNXbvrmWuFvPZuiDKwx98AgX/BoWNWLuq5ifS2dWYD3OdOU1QtGl7s/CWYCRuZ9k8OLBcQdQt+29TmKsGwajYwYCbm3I0fI4Qc+wGlkZkOpopf7SKwzX0q8AQIu/HCZV6rKLw9SC+MT19NQIDAQAB";
        String privateKey = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDUCWCC5piulfuc6QmMavzZRw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howWRolX5zkkyl2jdyTeeYidAXhpTgC/XDcKOwrArSeswQ4hfEfeCVGc8ycPPagf7pMS+7lpd/8TqZLg4j8GydZ2JAOVf6k+kJIcIvy0QnYsCgYBdl8nJiMBprZzWR8nXGgjrFe+kIAI3JMob0SYSaBcH4H7eluCvpYiL2/H8KsZ4ECdJwXQhULjeJ5Nd74VqYaVyZQRPv6o/UYitRvcokDFSdT8EnqQje1gsuw9fYMj0x0Er6/7YN5GUOhoyNqnMtv2XKm9qpsvr/nL/gVuaC4r3aQKBgQCBRn3owL1sZFn1CX2FPIC304Okex1FWgBqj8zTwYTd4SQLgPpIJijGCq7FmyH+JtxNM6URorU17vzLqboFqjpGiKfGD/f3+yd+z+L2j28MAEh83JArg49BT/jA0xDLOMNWlF4BjiFU5fFNhunV//SpBPODv9IopTXjS9a0nPkixw==";
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", "2019121369892336", privateKey, "json", "GBK", publicKey, "RSA2");
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setNotifyUrl("http://api.umashang.top/api/zfb/notify");
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", orderNo);
        bizContent.put("total_amount", amount);
        bizContent.put("subject", "测试商品");
        request.setBizContent(bizContent.toString());
        AlipayTradePrecreateResponse response = alipayClient.execute(request);
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
        String qrCode = response.getQrCode();
        System.out.println(qrCode);
        return qrCode;

    }

    public static String getBase64(String orderNo, String content) {
        //content="https://qr.alipay.com/bax05933rhh77y4wskzg3012";
        QrConfig qrConfig = new QrConfig();
        qrConfig.setWidth(250);
        qrConfig.setHeight(250);
        //String s = QrCodeUtil.generateAsBase64(content,qrConfig, "jpg");
//        File file = new File("C:\\Users\\le\\Desktop\\1111\\images\\1.jpg");
        String profile = RuoYiConfig.getProfile() + "/";

        File file = new File(profile + orderNo + ".jpg");
        //上传到服务器

        File generate = QrCodeUtil.generate(content, qrConfig, file);


        return null;
    }


}
